package com.wangc.report;

import com.wangc.report.dao.ConfigMapper;
import com.wangc.report.dao.LoadMapper;
import com.wangc.report.entity.ConfigEntity;
import com.wangc.report.entity.TaskLogEntity;
import com.wangc.report.service.LoadService;
import com.wangc.report.service.TaskLogService;
import com.wangc.report.service.impl.LoadServiceImpl;
import com.wangc.report.utils.RRException;
import com.wangc.report.utils.ReportCheck;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mybatis.spring.annotation.MapperScan;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import javax.annotation.Resource;
import java.util.Date;

@SpringBootTest
@RunWith(SpringRunner.class)
@MapperScan("com.wangc.report.dao")
public class test {
    private static final Logger LOG = LoggerFactory.getLogger(LoadServiceImpl.class);
    @Resource
    private LoadMapper loadMapper;
    @Resource
    private ConfigMapper configMapper;
    @Autowired
    private ReportCheck reportCheck;
    @Autowired
    private TaskLogService taskLogService;
    @Autowired
    private LoadService loadService;

    //@Test
    public void testmain (){
        System.out.println("++++start++++");
        if(null==loadService) System.out.println("**********");
        int table=6;
        for (int i = 0; i < 100; i++) {
            ConfigEntity configEntity = configMapper.selectByPrimaryKey(Long.valueOf(table));
            TaskLogEntity taskLogEntity = new TaskLogEntity();
            taskLogEntity.setTableId(configEntity.getTableId());
            taskLogEntity.setDataDt(String.valueOf(i));
            taskLogEntity.setTaskName("导入报表源文件");
            taskLogEntity.setNewOldFlg("");
            taskLogEntity.setProcStat("executeing");
            taskLogEntity.setState("0");
            taskLogEntity.setStartDate(new Date());
            taskLogService.save(taskLogEntity);
            try{
                reportCheck.loadSourceFile(configEntity.getTableId(), String.valueOf(i), "0");
                reportCheck.loadSourceFile(configEntity.getTableId(), String.valueOf(i), "1");
            }catch (Exception e){}
            taskLogEntity.setProcInfo("success");
            taskLogEntity.setProcStat("finish");
            taskLogEntity.setFinishDate(new Date());
            taskLogService.update(taskLogEntity);
            System.out.println(i);
        }
        System.out.println("++++over++++");
    }
    @Test
    public void testmain2 (){
        System.out.println("++++start++++");
        int table=6;
        for (int i = 0; i < 9; i++) {
            ConfigEntity configEntity = configMapper.selectByPrimaryKey(Long.valueOf(table));
            TaskLogEntity taskLogEntity = new TaskLogEntity();
            taskLogEntity.setTableId(configEntity.getTableId());
            taskLogEntity.setDataDt(String.valueOf(i));
            taskLogEntity.setTaskName("报表数据生成");
            taskLogEntity.setNewOldFlg("-");
            taskLogEntity.setProcStat("executeing");
            taskLogEntity.setState("0");
                try {
                    taskLogEntity.setStartDate(new Date());
                    taskLogService.save(taskLogEntity);
                    reportCheck.generateReport(configEntity.getTableId(), String.valueOf(i));
                } catch (Exception e) {
                    e.printStackTrace();
                    LOG.error(e.toString());
                    taskLogEntity.setProcStat("error");
                    taskLogEntity.setProcInfo(e.toString());
                    taskLogEntity.setFinishDate(new Date());
                    taskLogService.update(taskLogEntity);
                    throw new RRException(e.toString());
                }
                taskLogEntity.setProcInfo("success");
                taskLogEntity.setProcStat("finish");
                taskLogEntity.setFinishDate(new Date());
                taskLogService.update(taskLogEntity);

            System.out.println(i);
        }
        System.out.println("++++over++++");
    }
}
